/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormGame.java
 *
 * Created on Jun 7, 2011, 11:08:37 PM
 */

package heart_0812025;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author BAO
 */



public class FormGame extends javax.swing.JFrame implements Runnable{

    /** Creates new form FormGame */
    public FormGame() {
        initComponents();
        Frame[] frames = getFrames();
        Image img = new ImageIcon("src/Images/heart1.jpeg").getImage();
        panel_game = new ImagePanel(img);
        //panel_game.setLayout(new BorderLayout());
        frames[0].add(panel_game);
        Toolkit toolkit =  Toolkit.getDefaultToolkit ();
        Dimension dim = toolkit.getScreenSize();
        //Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        Insets scnMax = getToolkit().getScreenInsets(getGraphicsConfiguration());
        int taskBarSize = scnMax.bottom;
        frames[0].setSize(dim.width,dim.height-taskBarSize);
        //frames[0].setMinimumSize(dim);
        frames[0].setTitle("0812025");

        manager = new GameManager();
        manager.startNewGame();
        //Load anh cac la bai
        LoadImg();
        DrawCards();
        //Ve la bai ra manh hinh

        // khởi tạo 4 panel cho 4 chỗ chơi :
        
//        Image img1 = new ImageIcon("src/Images/Cards/0.jpg").getImage();
//        ImagePanel panel = new ImagePanel(img1);
//        Image img2 = new ImageIcon("src/Images/Cards/1.jpg").getImage();
//        ImagePanel panel2 = new ImagePanel(img2);
//        panel_game.add(panel, BorderLayout.CENTER);
//        panel_game.add(panel2, BorderLayout.SOUTH);
        // vẽ các bàn chơi
    }    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        menuitem_option = new javax.swing.JMenuItem();
        menuitem_exit = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        menuitem_help = new javax.swing.JMenuItem();
        menuitem_about = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(heart_0812025.Heart_0812025App.class).getContext().getResourceMap(FormGame.class);
        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N

        menuitem_option.setText(resourceMap.getString("menuitem_option.text")); // NOI18N
        menuitem_option.setName("menuitem_option"); // NOI18N
        jMenu2.add(menuitem_option);

        menuitem_exit.setText(resourceMap.getString("menuitem_exit.text")); // NOI18N
        menuitem_exit.setName("menuitem_exit"); // NOI18N
        jMenu2.add(menuitem_exit);

        jMenuBar1.add(jMenu2);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        menuitem_help.setText(resourceMap.getString("menuitem_help.text")); // NOI18N
        menuitem_help.setName("menuitem_help"); // NOI18N
        jMenu1.add(menuitem_help);

        menuitem_about.setText(resourceMap.getString("menuitem_about.text")); // NOI18N
        menuitem_about.setName("menuitem_about"); // NOI18N
        jMenu1.add(menuitem_about);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 781, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 386, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        Thread thread =new Thread(this);
        thread.start();
    }//GEN-LAST:event_formWindowOpened

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormGame().setVisible(true);
            }
        });
    }

    public JLabel[] CreateCards(Player player, boolean hidden)
    {
        JLabel[] listJLabel = new JLabel[13];
        if(hidden)
        {
            for(int i = 0; i<13; i++)
            {
                listJLabel[i] = new JLabel(this.listImgIcon[52]);
            }
        }
        else
        {
            for(int i = 0; i<13; i++)
            {
                int cardId = player.GetCard(i).GetID();
                int imgIndex = cardId - 1;
                listJLabel[i] = new JLabel(this.listImgIcon[imgIndex]);
            }
        }
        return listJLabel;
    }

    public void LoadImg()
    {
        listImgIcon = new ImageIcon[53];
        String imgPath;
        for(int i=0; i<52; i++ )
        {
            imgPath = "src/Images/Cards/" + Integer.toString(i + 1) + ".jpg";
            listImgIcon[i] = new ImageIcon(imgPath);
        }
        //Load la bai mat up
        imgPath = "src/Images/Cards/0.jpg";
        listImgIcon[52] = new ImageIcon(imgPath);
    }
    public void DrawCards()
    {
//        player1Labels = new JLabel[13];
//        player2Labels = new JLabel[13];
//        player3Labels = new JLabel[13];
//        player4Labels = new JLabel[13];
        player1Labels = CreateCards(manager.GetPlayer(1),false);
        player2Labels = CreateCards(manager.GetPlayer(2),true);
        player3Labels = CreateCards(manager.GetPlayer(3),true);
        player4Labels = CreateCards(manager.GetPlayer(4),true);
        int width = panel_game.getSize().width;
        int height = panel_game.getSize().height;
        int cardWidth = 100;
        int cardHeight = 135;
        int doDoi = 30;
        int left = 100;
        int right = 100;
        int top = 20;
        int bottom = 20;
        int p1Y = height - bottom - 200;
        int p1X = (width - (12*doDoi + cardWidth))/2;
        int p2Y = (height - (12*doDoi + cardHeight))/2;
        int p2X = left;
        int p3Y = top;
        int p3X = (width - (12*doDoi + cardWidth))/2;
        int p4Y = (height - (12*doDoi + cardHeight))/2;
        int p4X = width - right - 100;
        for(int i = 0; i<12; i++)
        {
            player1Labels[i].setBounds(p1X + i*doDoi, p1Y, cardWidth, cardHeight);
            player1Labels[i].setVisible(false);
            player3Labels[i].setBounds(p3X + i*doDoi, p3Y, cardWidth, cardHeight);
            player3Labels[i].setVisible(false);
            player2Labels[i].setBounds(p2X, p2Y + i*doDoi, cardWidth, cardHeight);
            player2Labels[i].setVisible(false);
            player4Labels[i].setBounds(p4X, p4Y + i*doDoi, cardWidth, cardHeight);
            player4Labels[i].setVisible(false);
            panel_game.add(player1Labels[i]);
            panel_game.add(player2Labels[i]);
            panel_game.add(player3Labels[i]);
            panel_game.add(player4Labels[i]);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem menuitem_about;
    private javax.swing.JMenuItem menuitem_exit;
    private javax.swing.JMenuItem menuitem_help;
    private javax.swing.JMenuItem menuitem_option;
    // End of variables declaration//GEN-END:variables

    GameManager manager;
    JLabel[] player1Labels, player2Labels, player3Labels, player4Labels;
    ImageIcon []listImgIcon;
    ImagePanel panel_game;


    public void run()
    {
        for(int i = 0; i<14; i++)
        {
            player1Labels[i].setVisible(true);
            player2Labels[i].setVisible(true);
            player3Labels[i].setVisible(true);
            player4Labels[i].setVisible(true);
            try
            {
                Thread.sleep(150);
            }
            catch (InterruptedException ex)
            {
                Logger.getLogger(FormGame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
